<meta charset="utf-8" xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<div id="nav-menu">
    <b-navbar toggleable="lg" type="dark" variant="dark">
        <b-navbar-nav>
            <b-nav-item-dropdown left>
                <!-- Using 'button-content' slot -->
                <template v-slot:button-content>
                    <em>{{systemName}}</em>
                </template>
                <b-dropdown-item v-for="menu in subsystemList" href="#" @click="getSubResource(menu)">{{menu.cnname}}</b-dropdown-item>
            </b-nav-item-dropdown>
        </b-navbar-nav>


        <b-navbar-toggle target="nav-collapse"></b-navbar-toggle>

        <b-navbar-nav >
            <template>
                <b-nav-item href="#" @click="homePage">首页</b-nav-item>
            </template>
        </b-navbar-nav>
            <b-navbar-nav>
                <template v-for="menu in menuList">
                    <b-nav-item v-if="!menu.childs || menu.childs.length <= 0" :href="menu.url" @click.prevent="handleClick($event, menu)">{{menu.name}}</b-nav-item>
                    <b-nav-item-dropdown v-else :href="menu.url"
                                         @show="toggleDrop($event, menu, true)"
                                         @hide="toggleDrop($event, menu, false)"
                                         :text="menu.name"
                                         @click="handleClick($event, menu)">
                        <template v-for="sub in menu.childs">
                            <b-dropdown-item v-if="!sub.childs || sub.childs.length <= 0" :href="sub.url"
                                             :style="{ 'float': 'left' }"
                                             @click.prevent="handleClick($event, sub)">{{sub.name}}</b-dropdown-item>
                            <b-dropdown-group v-else  :header="sub.name" :style="{ 'float': 'left' }">
                                <template v-for="subSub in sub.childs">
                                    <b-dropdown-item-button v-if="!subSub.childs || subSub.childs.length <= 0"
                                                            :href="subSub.url"
                                                            :style="{'max-width': '150px'}"
                                                            @click.prevent="handleClick($event, subSub)">{{subSub.name}}</b-dropdown-item-button>
                                    <b-dropdown-group v-else :header="subSub.name" :style="{ 'float': 'left' }">
                                        <b-dropdown-item-button v-for="subSub2 in subSub.childs" :href="subSub2.url"
                                                                :style="{'max-width': '150px'}"
                                                                @click.prevent="handleClick($event, subSub2)">{{subSub2.name}}</b-dropdown-item-button>
                                    </b-dropdown-group>
                                </template>
                            </b-dropdown-group>
                        </template>
                    </b-nav-item-dropdown>
                </template>
            </b-navbar-nav>

            <!-- Right aligned nav items -->
            <b-navbar-nav class="ml-auto">
                <b-nav-item-dropdown right>
                    <!-- Using 'button-content' slot -->
                    <template v-slot:button-content>
                        <em>{{userName}}</em>
                    </template>
                    <b-dropdown-item href="#" @click="modifyPwd">修改密码</b-dropdown-item>
                    <b-dropdown-item href="#" @click="changeTheme">更换风格</b-dropdown-item>
                    <b-dropdown-item href="#" @click="logout">退出登录</b-dropdown-item>
                </b-nav-item-dropdown>
                
            </b-navbar-nav>
        </b-collapse>
    </b-navbar>

    <b-modal v-model="show" :title="modelTitle" size="xl"
             scrollable
             no-close-on-backdrop
             hide-footer
             no-close-on-esc>
        <iframe name="modelIframe" :src="modelIframeUrl" style="min-height: 550px;"
                height="100%" width="100%" frameborder="0" scrolling="auto"></iframe>
    </b-modal>
</div>
<!-- /.container -->
<script type="text/javascript">
    var TOP = new Vue({
        el : '#nav-menu',
        data: {
            iframeListener: null,
            systemName: "批次调度平台控制台v3.0",
            subsystemList:[],
            userName: "",
            userId: "",
            _menuList: [
                {
                    name: "调度运行监控",
                    url: "#",
                    childs:[
                        {name:"调度运行监控", url:"batch-console/ScheduleMontior.html"},
                        {name:"批次运行监控", url:"batch-console/BatInstBatchList.html"},
                    ]
                },
                {
                    name: "调度配置管理",
                    url: "#",
                    childs:[
                        {
                            name:"调度配置",
                            url:"#",
                            childs:[
                                {name:"定时调度配置", url:"batch-console/SSrvsCronConfList.html"}
                            ]
                        },
                        {
                            name:"批次配置",
                            url:"#",
                            childs:[
                                {name: "批次配置", url:"batch-console/BatBatchInfoConfigList.html"},
                                {
                                    name: "批次参数配置",
                                    url:"#",
                                    childs: [
                                        {name:"任务阶段信息", url:"batch-console/BatBatchStageConfigList.html"},
                                        {name:"任务执行场所", url:"batch-console/BatTaskLocaleConfigList.html"},
                                        {name:"服务参数配置", url:"batch-console/PluginServiceParamList.html"},
                                    ]
                                },
                                {
                                    name: "批次插件配置",
                                    url:"#",
                                    childs: [
                                        {name:"任务插件信息", url:"batch-console/PluginDefineList.html"},
                                    ]
                                },
                            ]
                        },
                        {
                            name:"节点配置",
                            url:"#",
                            childs:[
                                { name: "执行节点配置", url: "batch-console/SAgentInfoList.html" }
                            ]
                        },
                    ]
                },
                {
                    name: "系统管理",
                    url: "#",
                    childs:[
                        {
                            name: "系统设置",
                            url: "#",
                            childs:[
                                {name: "系统基本信息", url: "batch-console/SSubsInfoList.html"},
                                {name: "系统数据源信息", url: "batch-console/SSubsDatasourceList.html"},
                                {name: "系统参数信息", url: "batch-console/SParamInfoList.html"},
                                {name: "系统字典信息", url: "admin/SDicList.html"},
                            ]
                        },
                        {
                            name: "组织机构",
                            url: "#",
                            childs:[
                                {name: "用户管理", url: "admin/SUserList.html"},
                                {name: "机构管理", url: "admin/SOrgList.html"},
                                {name: "角色管理", url: "admin/SRoleList.html"},
                                {name: "岗位管理", url: "admin/SRoleList.html"},
                            ]
                        },
                        {
                            name: "资源管理",
                            url: "#",
                            childs:[
                                {name: "资源定义", url: "admin/SResourceList.html"},
                                {name: "资源操作", url: "admin/SResourceactionList.html"},
                                {name: "资源权限", url: "admin/SRolerightList.html"},
                                {name: "子系统", url: "admin/SResourceList.html"},
                            ]
                        }
                    ]
                }
            ],
            menuList: [],

            modelIframeUrl: "",
            modelTitle: "",
            show: false,
        },
        computed: {
            rootPath(){
                var ROOT =  window.location.pathname.substr(0, window.location.pathname.indexOf("/", 1)) || window.location.origin;
                if (ROOT.startsWith("/page")) {
                    ROOT = window.location.origin;
                }
                return ROOT;
            },
            URL_PARAM(){
                return JSON.parse(window.localStorage.URL_PARAM) || {}
            },
            iframeMap(){
                return {
                    "batch-console/ScheduleMontior.html": true,
                    "batch-console/BatInstBatchList.html": true,
                    "batch-console/SSrvsCronConfList.html": true,
                    "batch-console/BatBatchInfoConfigList.html": true,
                    "batch-console/BatBatchStageConfigList.html": true,
                    "batch-console/BatTaskLocaleConfigList.html": true,
                    "batch-console/PluginServiceParamList.html": true,
                    "batch-console/PluginDefineList.html": true,
                    "batch-console/SAgentInfoList.html": true,
                }
            },
        },
        methods: {
            homePage(){
                this.iframeListener({name:"首页", url:"admin/HomePage.html"});
            },
            setIframeListener(listener) {
              this.iframeListener = listener
            },
            dealWithPath(url) {
                if (url.startsWith("/")) {
                    url = url.substr(1)
                } else {
                    url = url.startsWith('page') ?  url :`vue/${url}`;
                }
                return `${this.rootPath}/${url}`;
            },
            handleClick(e, menu) {
                if (this.isIframe(menu.url)) {
                    this.iframeListener.call(null, menu)
                } else {
                    this.go(url)
                }
            },
            go(url, param){
                if(url == '#') {
                    return
                } else {
                    if (param) {
                        window.localStorage.URL_PARAM = JSON.stringify(param)
                    }
                    let path = this.dealWithPath(url);
                    if (this.isIframe(url)) {
                        return path;
                    } else {
                        window.location.href = path;
                    }
                }
            },
            isIframe(url) {
                if (url.endsWith('login.html')) {
                    return false;
                }
                return true;
            },
            toggleDrop(even, menu, show) {
                console.log(even, menu, show);
                if (show) {
                    let hasGroup = $(even.target).find("li ul");
                    let width = menu.childs && hasGroup.length > 0 ? menu.childs.length * 150 : 160;
                    $(even.target).css("width", width + "px");
                } else {
                    $(even.target).css("width", "auto");
                }
            },
            logout(){
                JLEUtil.request("api/logout", "POST", null, null, (data)=>{
                    if(data.code == '0' && data.rows == true) {
                        this.go('/login.html');
                    } else {
                        this.$bvToast.toast(data.message, {
                            title: '提示 ',
                            toaster: 'b-toaster-top-center',
                            solid: true
                        })
                    }
                }, (jqXHR, textStatus, errorThrown)=>{
                    this.go('/login.html');
                });
            },
            modifyPwd(){
                this.modelIframeUrl = `admin/ModifyPassword.html`;
                this.modelTitle = '修改密码';
                this.show = true;
            },
            getSubResource(sub){
                this.$set(this,"systemName",sub.cnname);
                document.title=sub.cnname;
                JLEUtil.request(`api/s/resource/all/${sub.resourceid}`, 'POST', null, null, (data)=>{
                    let menuList=data.map((value, index)=>{
                        return {
                            name: value.text,
                            url: value.url || "#",
                            resourceid: value.id,
                            childs:value.children.map( chindVal=> this.recursiveMap(chindVal))
                        }
                    });
                this.$set(this, "menuList", menuList)
                });
            },
            getSubList(actorNo){
                JLEUtil.request('api/s/user/subs/info/'+actorNo, 'POST', null, null, (data)=>{
                	 let subsystem = data.rows || [];
                     let subsystemList = subsystem.map((value, index)=>{
                         return {
                             cnname: value.subsName,
                             url: value.url || "#",
                             resourceid: value.subsCode,
                             childs:[]
                         }
                     });
                     this.$set(this, "subsystemList", subsystemList);
                     this.getSubResource(this.subsystemList[0]);
                });
            },
            changeTheme(){
                this.modelIframeUrl = `../uistyle.html`;
                this.modelTitle = '更换风格';
                this.show = true;
            },
            loadUserMenu(){
                JLEUtil.request("api/admin/menus", "POST", null, {}, (data)=>{
                    this.userName = data.rows.actorName;
                    this.userId = data.rows.actorNo;
                    this.getSubList(data.rows.actorNo);
                }, (jqXHR, textStatus, errorThrown )=>{
                    this.go("/login.html");
                })
            },
            recursiveMap(node){
                return {
                    name: node.text,
                    url: node.url || "#",
                    resourceid: node.id,
                    parentId: node.parentId,
                    childs: node.children && node.children.length > 0 ? node.children.map(this.recursiveMap) : []
                }
            }
        },
        created(){
            this.loadUserMenu();
            // default listener
            this.setIframeListener((url)=>{
                window.location.href = this.go(`/index.html?target=${encodeURI(url)}`)
            })
        },
        mounted(){
            window.$TOP = this;
            $('title').text(this.systemName)
        }
    })

</script>
